import axios from 'axios'
import nProgress from 'nprogress'

axios.defaults.headers.post['Content-Type'] =
 'application/x-www-form-urlencoded'
const requests = axios.create({
  timeout: 5000
})

requests.interceptors.request.use((config) => {
  nProgress.start()
  return config
},
error => {
  console.log(error)
  return Promise.reject(error)
})

requests.interceptors.response.use(
  (res) => {
    nProgress.done()
    console.log(res)
    return res.data
  },
  (err) => {
    console.log(err)
    nProgress.done()
    alert('服务器响应数据失败')
  }
)

export default requests
